﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace codehuger
{
    public partial class mainForm : Form
    {
        model model;
        public mainForm()
        {
            InitializeComponent();
            combobox_init("huber");

            comboBox6.Items.Add("thinkphp");
            comboBox6.Items.Add("yii");

            comboBox7.Items.Add("public");
                comboBox7.Items.Add("protected");
                comboBox7.Items.Add("private");


        }

        private void combobox_init(string framework)
        {

            if (framework == "yii")
            {
                comboBox1.Items.Clear();

                comboBox2.Items.Clear();
                comboBox3.Items.Clear();
                comboBox4.Items.Clear();

                comboBox1.Items.Add("one");
                comboBox1.Items.Add("all");
                comboBox1.Items.Add("count");

                comboBox2.Items.Add("=");
                comboBox2.Items.Add("!=");
                comboBox2.Items.Add(">");
                comboBox2.Items.Add(">=");
                comboBox2.Items.Add("<");
                comboBox2.Items.Add("<=");
                comboBox2.Items.Add("like");

                comboBox3.Items.Add("=");
                comboBox3.Items.Add("!=");
                comboBox3.Items.Add(">");
                comboBox3.Items.Add(">=");
                comboBox3.Items.Add("<");
                comboBox3.Items.Add("<=");
                comboBox3.Items.Add("like");

                comboBox4.Items.Add("=");
                comboBox4.Items.Add("!=");
                comboBox4.Items.Add(">");
                comboBox4.Items.Add(">=");
                comboBox4.Items.Add("<");
                comboBox4.Items.Add("<=");
                comboBox4.Items.Add("like");


            }

            else
            {

                comboBox1.Items.Add("find");
                comboBox1.Items.Add("findAll");
                comboBox1.Items.Add("select");
                comboBox1.Items.Add("count");



                comboBox2.Items.Add("eq");
                comboBox2.Items.Add("neq");
                comboBox2.Items.Add("gt");
                comboBox2.Items.Add("egt");
                comboBox2.Items.Add("lt");
                comboBox2.Items.Add("elt");
                comboBox2.Items.Add("like");


                comboBox3.Items.Add("eq");
                comboBox3.Items.Add("neq");
                comboBox3.Items.Add("gt");
                comboBox3.Items.Add("egt");
                comboBox3.Items.Add("lt");
                comboBox3.Items.Add("elt");
                comboBox3.Items.Add("like");



                comboBox4.Items.Add("between");
                comboBox4.Items.Add("not between");
                comboBox4.Items.Add("in");
                comboBox4.Items.Add("not in");

                comboBox5.Items.Add("and");
                comboBox5.Items.Add("or");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string table_name, map, map1, map2, ret, result, order, condition, condition1, condition2, between_array, logic, fields_text, limit_text, page_text, group_text,
            having_text, join_text, union_text, distinct_text, lock_text, cache_text;

            string[] fields;

            // string str = "1,2,3,4,5,6,7";
            // string[] strArray = str.Split(','); //字符串转数组

            // foreach (string str1 in strArray)

            //     Console.WriteLine(str1);


            model = new model();

            fields_text = textBox8.Text;
            limit_text = textBox9.Text;
            page_text = textBox10.Text;
            group_text = textBox11.Text;
            having_text = textBox14.Text;
            join_text = textBox13.Text;
            union_text = textBox12.Text;
            distinct_text = textBox15.Text;
            lock_text = textBox16.Text;
            cache_text = textBox17.Text;

            order = textBox4.Text;

            model.table_name = textBox1.Text;
            condition = model.condition = comboBox2.Text;
            condition1 = model.condition1 = comboBox3.Text;
            condition2 = model.condition2 = comboBox4.Text;
            map = model.map = textBox2.Text;
            map1 = model.map1 = textBox5.Text;
            map2 = model.map2 = textBox6.Text;

            between_array = model.between_array = textBox7.Text;
            logic = model.logic = comboBox5.Text;
            model.ret = comboBox1.Text;

            //model.result = "M(\"" + model.table_name + "\",null)->where(array(\""+ model.map+"\" => \"parama\"))->";

            model.result = "$Model=M(\"" + model.table_name + "\",null);\r\n";

            if (comboBox6.Text == "yii")
            {
                model.result = "$Model=new " + model.table_name + "();\r\n";

                model.result += "$list=$Model::find()\r\n";

                model.result += "->andWhere(['" + condition + "','id',10])\r\n";
                model.result += "->andWhere(['" + condition1 + "','id',10])\r\n";
                model.result += "->andWhere(['" + condition2 + "','id',10])\r\n";

                model.result += "->" + model.ret + "();";

            }

            else
            {

                model.result += "$map['" + map + "']  = array('" + condition + "',100);\r\n";
                model.result += "$map['" + map1 + "']  = array('" + condition1 + "',100);\r\n";
                if (map2 != "")
                {
                    model.result += "$map['" + map2 + "']  = array('" + condition2 + "','" + between_array + "');\r\n";

                }
                if (logic != "")
                {
                    model.result += "$map['_logic'] = '" + logic + "';\r\n";

                }


                model.result += "$list=$Model->where($map)->";


                if (fields_text != "")
                {
                    fields = fields_text.Split(',');

                    string fields_e = string.Empty;


                    //foreach (string str2 in fields)

                    // Console.WriteLine(str2);

                    List<string> list_field = new List<string>();
                    foreach (string str2 in fields)
                        // Console.WriteLine(str2);
                        list_field.Add("" + str2 + "");

                    string[] _list_field = list_field.ToArray();

                    foreach (string str12 in _list_field)

                        Console.WriteLine(str12);

                    fields_e = string.Join(",", _list_field);//数组转成字符串

                    model.result += "field('" + fields_e + "')->";

                }

                if (limit_text != "")
                {
                    fields = limit_text.Split(',');

                    string fields_e = string.Empty;


                    //foreach (string str2 in fields)

                    // Console.WriteLine(str2);

                    List<string> list_field = new List<string>();
                    foreach (string str2 in fields)
                        // Console.WriteLine(str2);
                        list_field.Add(str2);

                    string[] _list_field = list_field.ToArray();

                    foreach (string str12 in _list_field)

                        Console.WriteLine(str12);

                    fields_e = string.Join(",", _list_field);//数组转成字符串

                    model.result += "limit('" + fields_e + "')->";

                }

                if (page_text != "")
                {


                    model.result += "page('" + page_text + "')->";

                }

                if (group_text != "")
                {


                    model.result += "group('" + group_text + "')->";

                }
                if (having_text != "")
                {


                    model.result += "having('" + having_text + "')->";

                }
                if (join_text != "")
                {


                    model.result += "join('" + join_text + "')->";

                }
                if (union_text != "")
                {


                    model.result += "union('" + union_text + "')->";

                }

                if (distinct_text != "")
                {


                    model.result += "distinct(true)->";

                }

                if (lock_text != "")
                {


                    model.result += "lock(true)->";

                }

                if (cache_text != "")
                {


                    model.result += "cache(true)->";

                }


                // model.result += "->where(array(\"";
                // model.result += model.map;
                // model.result += "\" => \"parama\"))->";

                model.result += "order(\"" + order + "\")->";

                if (model.ret != "")
                {


                    switch (model.ret)
                    {
                        case "getField":
                            model.ret = "id";
                            model.result += "getField(\"" + model.ret + "\");";
                            break;

                        /* 您可以有任意数量的 case 语句 */
                        default: /* 可选的 */
                            model.result += model.ret + "();";
                            break;
                    }


                }
                else
                {
                    model.result += "select();";

                }
            }

            // model.result += model.ret;

            // model.result += "\");";

            textBox3.Text = model.result;




        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();

            comboBox2.Items.Clear();
            comboBox3.Items.Clear();
            comboBox4.Items.Clear();
            combobox_init(comboBox6.Text);
            Console.WriteLine(comboBox6.Text);
            if (comboBox6.Text == "yii")
            {

            }

        }

        private void button2_Click(object sender, EventArgs e)
        {

            string function_name, param1, param2, ret,res;

            function_name = textBox18.Text;

            param1 = textBox19.Text;
            param2 = textBox20.Text;
            ret = textBox21.Text;
            res="";
            res += comboBox7.Text+" function %sp_get_nickname%($@user_id@) {\r\n";
            Regex reg = new Regex(@"\%[^%]+\%");
            if(comboBox7.Text=="protected"||comboBox7.Text=="private"){

                function_name="_"+function_name;
            }
            res = reg.Replace(res, function_name);

             Regex reg1 = new Regex(@"\@[^@]+\@");
            res = reg1.Replace(res, param1);

            res += "return $%res%;\r\n";
            Regex reg2 = new Regex(@"\%[^%]+\%");
            res = reg2.Replace(res, ret);

            res+= "}\r\n";

            textBox22.Text=res;





            //             function sp_get_nickname($user_id) {
            //    $res=D('User')->where(array('user_id' =>$user_id))->getField('nickname');
            //     return $res;
            // }




        }

        private void textBox22_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

            string class_name,function_name, param1, param2, ret,res;

            class_name = textBox24.Text;
            function_name = textBox25.Text;

            param1 = textBox26.Text;
            param2 = textBox27.Text;
            ret = textBox21.Text;
            res="";

            res +="class Person {\r\n";
            res +="  var $name;\r\n";
            res +="  var $age;\r\n";

            //定义一个构造方法初始化赋值
            res +="  function __construct($name,  $age) {\r\n";
            res +="    $this->name=$name;\r\n";
            res +="    $this->age=$age;\r\n";
            res +="  }\r\n";

            res +="  function say() {\r\n";
            res +="    echo '我的名字叫：'.$this->name.'<br />';\r\n";
            res +="    echo '我的年龄是：'.$this->age;\r\n";
            res +="  }\r\n";
            res +="}\r\n";

            res +="$p1=new Person('张三', 20);\r\n";
            res +="$p1->say();";

          res = res.Replace("say", function_name).Replace("Person", class_name).Replace("name", param1).Replace("age", param2);

            textBox23.Text=res;

            }
        }
}
